<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Created by htmlize-1.16 in css mode. -->
<html>
  <head>
    <title>csvformat.pl</title>
    <style type="text/css">
    <!--
      body {
        color: #000000;
        background-color: #f5deb3;
        font-weight: bold;
      }
      .comment {
        /* font-lock-comment-face */
        color: #b22222;
      }
      .cperl-array {
        /* cperl-array-face */
        color: #0000ff;
        background-color: #eeeed1;
        font-weight: bold;
      }
      .cperl-nonoverridable {
        /* cperl-nonoverridable-face */
        color: #66cd00;
      }
      .keyword {
        /* font-lock-keyword-face */
        color: #a020f0;
      }
      .string {
        /* font-lock-string-face */
        color: #bc8f8f;
      }
      .type {
        /* font-lock-type-face */
        color: #228b22;
      }
      a {
        color: inherit;
        background-color: inherit;
        font: inherit;
        text-decoration: inherit;
      }
      a:hover {
        text-decoration: underline;
      }
    -->
    </style>
  </head>
  <body>
    <pre>
<span class="comment">#!perl -w
</span>
<span class="comment"># Time-stamp: &lt;2002/04/06, 13:12:13 (EST), maverick, csvformat.pl&gt;
# Two pass CSV file to table formatter
</span>
$delim = <span class="cperl-array">$#ARGV</span> &gt;= 1 ? <span class="cperl-array">$ARGV</span>[1] : <span class="string">','</span>;
<span class="cperl-nonoverridable">print</span> STDERR <span class="string">"Split pattern: $delim\n"</span>;

<span class="comment"># first pass
</span><span class="type">open</span> F, <span class="string">"&lt;</span><span class="cperl-array">$ARGV</span><span class="string">[0]"</span> <span class="type">or</span> <span class="keyword">die</span>;
<span class="keyword">while</span>(&lt;F&gt;)
{
  <span class="cperl-nonoverridable">chomp</span>;
  $i = 0;
  <span class="cperl-nonoverridable">map</span> { <span class="cperl-array">$max</span>[$_-&gt;[1]] = $_-&gt;[0] <span class="keyword">if</span> $_-&gt;[0] &gt; (<span class="cperl-array">$max</span>[$_-&gt;[1]] || 0) }
    (<span class="cperl-nonoverridable">map</span> {[<span class="type">length</span> $_, $i++]} <span class="cperl-nonoverridable">split</span>($delim));
}
<span class="type">close</span> F;

<span class="cperl-nonoverridable">print</span> STDERR <span class="string">'Field width:   '</span>, <span class="type">join</span>(<span class="string">', '</span>, <span class="cperl-array">@max</span>), <span class="string">"\n"</span>;
<span class="cperl-nonoverridable">print</span> STDERR <span class="type">join</span>(<span class="string">' '</span>, <span class="cperl-nonoverridable">map</span> {<span class="string">'-'</span> <span class="type">x</span> $_} <span class="cperl-array">@max</span>);

<span class="comment"># second pass
</span><span class="type">open</span> F, <span class="string">"&lt;</span><span class="cperl-array">$ARGV</span><span class="string">[0]"</span> <span class="type">or</span> <span class="keyword">die</span>;
<span class="keyword">while</span>(&lt;F&gt;)
  {
  <span class="cperl-nonoverridable">chomp</span>;
  $i = 0;
  <span class="cperl-nonoverridable">map</span> { <span class="cperl-nonoverridable">printf</span>(<span class="string">"%-</span><span class="cperl-array">$max</span><span class="string">[$_-&gt;[1]]s "</span>, $_-&gt;[0]) }
    (<span class="cperl-nonoverridable">map</span> {[$_, $i++]} <span class="cperl-nonoverridable">split</span>($delim));
  <span class="cperl-nonoverridable">print</span> <span class="string">"\n"</span>;
}
<span class="type">close</span> F;
</pre>
  </body>
</html>
